<?php
$_session = Mage::getSingleton('customer/session');
$_customer = $_session->getCustomer();
$_orders = Mage::getModel('sales/order')->getCollection()
        ->addFieldToFilter('customer_id', array('eq'=>$_customer->getId()))
        ->setOrder('entity_id')->setPage(1,3);

$_productCollection = array();
$ids = array();
foreach($_orders as $_order)
{
    //$ids = array();
    $_items = $_order->getItemsCollection();
    foreach($_items as $item)
    {
        $ids[] = $item->getProductId();
    }
}

$_productCollection = Mage::getModel("catalog/product")->getCollection()
    ->addAttributeToSelect('*')
    ->addWebsiteFilter(Mage::app()->getWebsite())
    ->addAttributeToFilter('entity_id', array('in'=>$ids))
    ->addAttributeToFilter('status', array('eq' => Mage_Catalog_Model_Product_Status::STATUS_ENABLED));

$_currentCategory = Mage::registry('current_category');
if(!$_currentCategory) Mage::register('current_category',Mage::getModel('catalog/category')->load(12));

$catalogMod  = Mage::getModel('catalog/category');
$_helper = $this->helper('catalog/output');
$_catalogHp  = $this->helper('catalog/category');
$zgbHelper = Mage::helper('zgb');
?>
<div  class="row main">
    <ul class="breadcrumbs">
        <li>
            <a href="<?php echo Mage::getBaseurl() ?>" title="前往主页">主页</a>
            <span>></span>
        </li>
        <li><strong>最近购买</strong></li>
    </ul>

    <div class="product-screen">
        <div class="nav">
            <div class="row title">
                <h3>最近购买</h3>
                <h4><a href="/index.php/catalog/category/view/id/12">商品分类</a></h4>
            </div>
            <?php if(!$_productCollection->count()): ?>
                <p class="note-msg"><?php echo $this->__('您最近没有购买任何产品.') ?></p>
            <?php else : ?>
                <div class="row product-section recent">
                    <div class="line"></div>
                    <ul class="product-list">
                    <?php foreach($_productCollection as $_product): ?>
                        <li>
                            <div class="product-wrap">
                                <div class="product-image">
                                    <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>">
                                        <img class="lazy" src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(210); ?>" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>"/>
                                    </a>
                                    <?php 
                                        $proid = $_product->getId();
                                        $pro = Mage::getModel('catalog/product')->load($proid);
                                    ?>
                                    <?php if($zgbHelper->hasTag($_product)): ?>
                                        <div class="product-sale-tag">
                                            <img src="<?php echo $zgbHelper->getSaleTagUrl($_product, $this->getSkinUrl('images')); ?>" alt="">
                                        </div>
                                    <?php endif; ?>
                                </div>
                                <div class="product-shop">
                                    <h3><?php echo $_helper->productAttribute($_product, $_product->getName(), 'name') ?></h3>
                                    <div class="row">
                                         <?php if(number_format($_product->getPrice(),2) <= number_format($_product->getFinalPrice(),2)): ?>
                                            <span class="col-2 special-price">
                                                ￥&nbsp;<?php echo number_format($_product->getPrice(),2); ?>
                                            </span>
                                        <?php else : ?>
                                            <span class="col-2 old-price" >
                                                ￥&nbsp;<?php echo number_format($_product->getPrice(),2); ?>
                                            </span>
                                            <span class="col-2 special-price">
                                                ￥&nbsp;<?php echo number_format($_product->getFinalPrice(),2); ?>
                                            </span>
                                        <?php endif; ?>
                                    </div>
                                    <div class="row fill-box" id="datactions" data-action="<?php echo $this->getUrl('checkout/cart/addAjax',array('product'=>$_product->getId())); ?>">
                                        <div class="col-2 qty-area">
                                            <span class="reduce">-</span>
                                            <div class="qty-input">
                                                <input type="text" name="qty" id="qty" value="1"/>
                                            </div>
                                            <span class="add">+</span>
                                        </div>
                                        <div class="col-2">
                                            <button class="add-cart-button">加入购物车</button>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </li>
                    <?php endforeach; ?>
                    </ul>
                </div>
                <div class="line"></div>
            <?php endif; ?>
        </div>
    </div>
</div>